﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Beyondbit.Desktop.Common;
using RBS.Common;
using RBS.Common.JsonEntity;

namespace RBS.Admin.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }
        #region 登录
        /// <summary>
        /// Logins the specified return URL.
        /// </summary>
        /// <param name="returnUrl">The return URL.</param>
        /// <param name="form">The form.</param>
        /// <returns></returns>
       [AcceptVerbs(HttpVerbs.Post)] 
        public JsonResult UserLogin(string returnUrl, FormCollection form)
        {
            string uid = form["txtUserName"];
            string password = form["txtPassword"];
            var ret = new JsonReturnMessages();
         
            if (uid.Equals("") || password.Equals(""))
            {
                ret.IsSuccess = false;
                ret.Msg = "用户名或密码错误！请检查后重试";
                return Json(ret);
            }
            if (Session["AdminLoginSun"] == null)
                 Session["AdminLoginSun"] = 1;
           
            //判断登录错误次数
            if (Session["AdminLoginSun"] != null && Convert.ToInt32(Session["AdminLoginSun"]) > 5)
            {
                ret.IsSuccess = false;
                ret.Msg = "错误超过5次，关闭浏览器重新登录！";
                return Json(ret);
            }
            
                var bll = new Core.BLL.manager();
                var model = bll.GetModel(uid, password, true);
                if (model == null)
                {
                    ret.IsSuccess = false;
                    ret.Msg = "用户名或密码错误！请检查后重试";
                    Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
                    return Json(ret);
                }
               
                    Session[RBSKeys.SESSION_ADMIN_INFO] = model;
                    Session.Timeout = 45;
                    //写入登录日志 
                    new Core.BLL.manager_log().Add(model.id, model.user_name, RBSEnums.ActionEnum.Login.ToString(), "用户登录");
                    //写入Cookies
                    Utils.WriteCookie("DTRememberName", model.user_name, 14400);
                    Utils.WriteCookie("AdminName", "DTcms", model.user_name);
                    Utils.WriteCookie("AdminPwd", "DTcms", model.password);
                    if (!User.Identity.IsAuthenticated)
                    {
                        FormsAuthentication.SignOut();
                    }
                    ret.IsSuccess = true;
                    ret.Msg = "操作成功";
                    FormsAuthentication.SetAuthCookie(uid, false); 
                    ret.Data = !string.IsNullOrEmpty(returnUrl) ? returnUrl : Url.Action("Index","Home");
                    return Json(ret);
        }
        #endregion


    }
}
